package com.lebang.service.cences;

import com.lebang.domain.CencesBrandEffect;
import com.lebang.domain.TaskRouse;
import com.lebang.example.CencesBrandEffectExample;
import com.lebang.mapper.*;
import com.lebang.model.MyConfig;
import com.lebang.model.cences.CencesMsg;
import com.lebang.model.cences.DownloadMemberRecordCount;
import com.lebang.model.cences.RouseClickRecordCount;
import com.lebang.service.BaseService;
import com.lebang.service.mongodb.CencesMsgMongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 新手任务
 *
 * @Author xwj
 * @Date 2020-04-09
 */
@Service
public class CencesBrandEffectService extends BaseService<CencesBrandEffect, Integer, CencesBrandEffectExample> {
    @Autowired
    private CencesBrandEffectMapper baseMapper;

    @Autowired
    private DownloadMemberRecordMapper downloadMemberRecordMapper;

    @Autowired
    private MemberClidkRouseRecordMapper memberClidkRouseRecordMapper;
    @Autowired
    private LbbMemberMapper lbbMemberMapper;

    @Autowired
    private TaskRouseMapper taskRouseMapper;

    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;

    public void starDataInit(LocalDateTime end) {
        this.starDataInit(end, null);
    }

    /**
     * 统计指定日期的渠道效果
     *
     * @param end   结束日期
     * @param brand 渠道
     */
    public void starDataInit(LocalDateTime end, List<String> brand) {
        long starTime = System.currentTimeMillis();
        //star 开始日期  end 结束日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDateTime star = end.minusDays(1);

        //所有的渠道来源
        if (brand == null) {
            brand = lbbMemberMapper.selectBrand();
        }

        //所有的数据源的sdkkey
        List<TaskRouse> sdkKeyList = taskRouseMapper.selectByExample(null);

        //转换成yyyyMMdd格式
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));

        //签到量
        AtomicInteger clickNum = new AtomicInteger(0);

        //全渠道全数据源
        CencesBrandEffect allBrandAllSdk = new CencesBrandEffect(time, "all", "all", 1, MyConfig.getEnv());


        List<DownloadMemberRecordCount> downloadMemberRecordCount  = downloadMemberRecordMapper.countByTime(star, end);
        downloadMemberRecordCount.forEach(s-> s.setAppSdk(s.getAppStore()+":"+s.getSdkKey()));
        Map<String,List<DownloadMemberRecordCount>> data =  downloadMemberRecordCount.stream().collect(Collectors.groupingBy(DownloadMemberRecordCount::getAppSdk));


        List<RouseClickRecordCount> rouseClickRecordCount = memberClidkRouseRecordMapper.countClickNum(star, end);
        rouseClickRecordCount.forEach(s-> s.setAppRouseDays(s.getAppStore()+":"+s.getRouseId()+":"+s.getDays()));
        Map<String,List<RouseClickRecordCount>> clickData = rouseClickRecordCount.stream().collect(Collectors.groupingBy(RouseClickRecordCount::getAppRouseDays));


        //分渠道
        for (String s : brand) {
            //单渠道总数据源
            CencesBrandEffect singleBrandAllSdk = new CencesBrandEffect(time, s, "all", 0,MyConfig.getEnv());
            Integer singleBrandAllSdkFinish = 0;
            Integer singleBrandAllSdkOneClick = 0;
            Integer singleBrandAllSdkTwoClick = 0;
            Integer singleBrandAllSdkThreeClick = 0;
            Integer singleBrandAllSdkFourClick = 0;
            Integer singleBrandAllSdkFiveClick = 0;

            for (TaskRouse sdk : sdkKeyList) {


                //单渠道单数据源
                CencesBrandEffect singleBrandSingleSdk = new CencesBrandEffect(time, s, sdk.getSdkKey(), 0,MyConfig.getEnv());
                //单渠道单数据源每日必赚完成量
                singleBrandSingleSdk.setFinish(countFinish(data,s, sdk.getSdkKey()));
                //单渠道单数据源签到量 次日留存
                clickNum.set(1);
                singleBrandSingleSdk.setOneClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                //单渠道单数据源签到量 2-4留存
                clickNum.set(2);
                singleBrandSingleSdk.setTwoClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                //单渠道单数据源签到量 5-7留存
                clickNum.set(3);
                singleBrandSingleSdk.setThreeClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                //单渠道单数据源签到量 12-14留存
                clickNum.set(4);
                singleBrandSingleSdk.setFourClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                //单渠道单数据源签到量 30+留存
                clickNum.set(5);
                singleBrandSingleSdk.setFiveClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                baseMapper.insertBrandEffect(singleBrandSingleSdk);

                singleBrandAllSdkFinish = singleBrandSingleSdk.getFinish() + singleBrandAllSdkFinish;
                singleBrandAllSdkOneClick = singleBrandSingleSdk.getOneClick() + singleBrandAllSdkOneClick;
                singleBrandAllSdkTwoClick = singleBrandSingleSdk.getTwoClick() + singleBrandAllSdkTwoClick;
                singleBrandAllSdkThreeClick = singleBrandSingleSdk.getThreeClick() + singleBrandAllSdkThreeClick;
                singleBrandAllSdkFourClick = singleBrandSingleSdk.getFourClick() + singleBrandAllSdkFourClick;
                singleBrandAllSdkFiveClick = singleBrandSingleSdk.getFiveClick() + singleBrandAllSdkFiveClick;
            }
            singleBrandAllSdk.setFinish(singleBrandAllSdkFinish);
            singleBrandAllSdk.setOneClick(singleBrandAllSdkOneClick);
            singleBrandAllSdk.setTwoClick(singleBrandAllSdkTwoClick);
            singleBrandAllSdk.setThreeClick(singleBrandAllSdkThreeClick);
            singleBrandAllSdk.setFourClick(singleBrandAllSdkFourClick);
            singleBrandAllSdk.setFiveClick(singleBrandAllSdkFiveClick);

            baseMapper.insertBrandEffect(singleBrandAllSdk);

        }


        //分数据源
        for (TaskRouse sdk : sdkKeyList) {
            //全渠道单数据源
            CencesBrandEffect allBrandSingleSdk = new CencesBrandEffect(time, "all", sdk.getSdkKey(), 1,MyConfig.getEnv());
            Integer allBrandsingleSdkFinish = 0;
            Integer allBrandsingleSdkOneClick = 0;
            Integer allBrandsingleSdkTwoClick = 0;
            Integer allBrandsingleSdkThreeClick = 0;
            Integer allBrandsingleSdkFourClick = 0;
            Integer allBrandsingleSdkFiveClick = 0;

            for (String s : brand) {


                //单渠道单数据源
                CencesBrandEffect singleBrandSingleSdk = new CencesBrandEffect(time, s, sdk.getSdkKey(), 0,MyConfig.getEnv());
                //单渠道单数据源每日必赚完成量
                singleBrandSingleSdk.setFinish(countFinish(data,s, sdk.getSdkKey()));
                //单渠道单数据源签到量 次日留存
                clickNum.set(1);
                singleBrandSingleSdk.setOneClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                //单渠道单数据源签到量 2-4留存
                clickNum.set(2);
                singleBrandSingleSdk.setTwoClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                //单渠道单数据源签到量 5-7留存
                clickNum.set(3);
                singleBrandSingleSdk.setThreeClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                //单渠道单数据源签到量 12-14留存
                clickNum.set(4);
                singleBrandSingleSdk.setFourClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                //单渠道单数据源签到量 30+留存
                clickNum.set(5);
                singleBrandSingleSdk.setFiveClick(countClick(clickData,clickNum.get(),sdk.getId().intValue(), s));

                allBrandsingleSdkFinish = singleBrandSingleSdk.getFinish() + allBrandsingleSdkFinish;
                allBrandsingleSdkOneClick = singleBrandSingleSdk.getOneClick() + allBrandsingleSdkOneClick;
                allBrandsingleSdkTwoClick = singleBrandSingleSdk.getTwoClick() + allBrandsingleSdkTwoClick;
                allBrandsingleSdkThreeClick = singleBrandSingleSdk.getThreeClick() + allBrandsingleSdkThreeClick;
                allBrandsingleSdkFourClick = singleBrandSingleSdk.getFourClick() + allBrandsingleSdkFourClick;
                allBrandsingleSdkFiveClick = singleBrandSingleSdk.getFiveClick() + allBrandsingleSdkFiveClick;

            }
            allBrandSingleSdk.setFinish(allBrandsingleSdkFinish);
            allBrandSingleSdk.setOneClick(allBrandsingleSdkOneClick);
            allBrandSingleSdk.setTwoClick(allBrandsingleSdkTwoClick);
            allBrandSingleSdk.setThreeClick(allBrandsingleSdkThreeClick);
            allBrandSingleSdk.setFourClick(allBrandsingleSdkFourClick);
            allBrandSingleSdk.setFiveClick(allBrandsingleSdkFiveClick);
            baseMapper.insertBrandEffect(allBrandSingleSdk);

            sumAllSdk(allBrandSingleSdk, allBrandAllSdk);

        }

        baseMapper.insertBrandEffect(allBrandAllSdk);


        cencesMsgMongodb.save(new CencesMsg("渠道效果统计",(System.currentTimeMillis() - starTime)));
    }




    /**
     * 计算所有渠道所有数据源
     *
     * @param allBrandSingleSdk
     * @param allBrandAllSdk
     */
    private void sumAllSdk(CencesBrandEffect allBrandSingleSdk, CencesBrandEffect allBrandAllSdk) {
        allBrandAllSdk.setFinish(allBrandAllSdk.getFinish() + allBrandSingleSdk.getFinish());
        allBrandAllSdk.setOneClick(allBrandAllSdk.getOneClick() + allBrandSingleSdk.getOneClick());
        allBrandAllSdk.setTwoClick(allBrandAllSdk.getTwoClick() + allBrandSingleSdk.getTwoClick());
        allBrandAllSdk.setThreeClick(allBrandAllSdk.getThreeClick() + allBrandSingleSdk.getThreeClick());
        allBrandAllSdk.setFourClick(allBrandAllSdk.getFourClick() + allBrandSingleSdk.getFourClick());
        allBrandAllSdk.setFiveClick(allBrandAllSdk.getFiveClick() + allBrandSingleSdk.getFiveClick());
    }


    /**
     * 统计每日必赚完成量
     * @param map 完成量
     * @param appStore 渠道
     * @param sdkKey 数据源
     * @return 返回内容
     */
    private Integer countFinish(Map<String,List<DownloadMemberRecordCount>> map, String appStore, String sdkKey) {
        AtomicInteger a=new AtomicInteger(0);
        List<DownloadMemberRecordCount> downloadMemberRecordCountList = map.get(appStore+":"+sdkKey);
        if(null==downloadMemberRecordCountList){
            return a.get();
        }
        downloadMemberRecordCountList.forEach(s->{
            a.addAndGet(s.getCount());
        });
        return a.get();
    }

    /**
     * 统计签到量
     * @param clickData 签到量
     * @param days 签到数
     * @param rouseId 数据源id
     * @param appStore 渠道
     * @return
     */
    private Integer countClick(Map<String, List<RouseClickRecordCount>> clickData, int days, int rouseId, String appStore) {
       AtomicInteger a=new AtomicInteger(0);
        List<RouseClickRecordCount> rouseClickRecordCountList= clickData.get(appStore+":"+rouseId+":"+days);
        if(null==rouseClickRecordCountList){
            return a.get();
        }
        rouseClickRecordCountList.forEach(s->{
            a.addAndGet(s.getCount());
        });
        return a.get();
    }

    public void deleteByTime(Integer time) {
        CencesBrandEffectExample cencesBrandEffectExample = new CencesBrandEffectExample();
        cencesBrandEffectExample.createCriteria().andTimeEqualTo(time).andAppIdEqualTo(MyConfig.getEnv());
        baseMapper.deleteByExample(cencesBrandEffectExample);
    }
}
